package com.wedo.mall.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.EUtils;
import com.e.library.widget.ELabelValueView;
import com.hyphenate.easeui.ui.EaseGMapActivity;
import com.wedo.mall.R;
import com.wedo.mall.WApp;
import com.wedo.mall.util.WUtils;
import com.wedo.mall.widget.LabelInputView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import me.iwf.photopicker.PhotoPicker;

/**
 * 商家注册-补充材料
 * Created by lxy on 2017/6/15.
 */

public class AgentInfoActivity extends EActivity {

    private static final int REQ_CATEGORY = 100;
    private static final int REQ_LOCATION = 101;
    @BindView(R.id.iv_licence) ImageView iv_licence;
    @BindView(R.id.tv_licence) TextView tv_licence;

    @BindView(R.id.liv_company) LabelInputView      liv_company;
    @BindView(R.id.liv_register_id) LabelInputView  liv_register_id;
    @BindView(R.id.liv_address) LabelInputView      liv_address;
    @BindView(R.id.liv_manager) LabelInputView      liv_manager;
    @BindView(R.id.liv_phone) LabelInputView        liv_phone;
    @BindView(R.id.liv_email) LabelInputView        liv_email;
    @BindView(R.id.liv_mobile) LabelInputView       liv_mobile;
    @BindView(R.id.lvv_category) ELabelValueView    lvv_category;

    @BindString(R.string.register_pls_enter) String register_pls_enter;

    private String img;
    double latitude = 0;
    double longitude = 0;

    @Override
    protected int layout() {
        return R.layout.activity_agent_info;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        liv_address.et_value.setPadding(0,0,40,0);
    }

    private boolean validation(){

        if(liv_company.isEmpty()) {
            toastEnter(liv_company);
            return false;
        }

        if(liv_register_id.isEmpty()) {
            toastEnter(liv_register_id);
            return false;
        }

        if(liv_address.isEmpty()) {
            toastEnter(liv_address);
            return false;
        }

        if (latitude == 0 || longitude == 0) {
            toast(R.string.map_agent_geo_invalid);
            return false;
        }

        if(liv_manager.isEmpty()) {
            toastEnter(liv_manager);
            return false;
        }

        if(liv_phone.isEmpty()) {
            toastEnter(liv_phone);
            return false;
        }

        String phone = liv_phone.text();
        if(!EUtils.isMobile(phone)) {
            toast(R.string.register_phone_invalid);
            return false;
        }

        if(liv_email.isEmpty()) {
            toastEnter(liv_email);
            return false;
        }

        String email = liv_email.text();
        if(!EUtils.isEmail(email)) {
            toast(R.string.register_email_invalid);
            return false;
        }

        if(lvv_category.getTag() == null) {
            toast(R.string.register_agent_category_tips);
            return false;
        }

        if(TextUtils.isEmpty(img)) {
            toast(R.string.register_select_license_tips);
            return false;
        }

        return true;
    }

    private void toastEnter(LabelInputView liv){
        String toast = String.format(register_pls_enter,liv.label());
        toast(toast);

    }

    @OnClick(R.id.btn_submit)
    protected void onSubmitClick(){
        if(!validation()) {
            return;
        }
        loading();
        upload();
    }

    private void upload(){
        String imgStr = WUtils.toBase64(img);
        Observable<EResponse> observable = api().upload(imgStr,"Agent");
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                String image = response.string("image");
                submit(image);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    private void submit(String license){
        Map<String,String> params = new HashMap<>(10);

        String userId = getIntent().getStringExtra("userId");
        params.put("userId",userId);
        params.put("license",license);
        params.put("company_name",liv_company.text());
        params.put("registr_id",liv_register_id.text());
        params.put("address",liv_address.text());
        params.put("longitude",String.valueOf(longitude));
        params.put("latitude",String.valueOf(latitude));
        params.put("manager",liv_manager.text());
        params.put("email",liv_email.text());
        params.put("telephone",liv_phone.text());
        params.put("categoryids",lvv_category.getTag().toString());

        if(!liv_mobile.isEmpty()) {
            params.put("agent_phone",liv_mobile.text());
        }

        post(EApi.AGENT_COMPLETE, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                defaultFinish();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });

    }

    @OnClick(R.id.lvv_category)
    protected void onCategoryClick(){
        toActivityForResult(AgentCategoryActivity.class,REQ_CATEGORY);
    }

    @OnClick(R.id.ll_licence)
    protected void onLicenceClick(){
        PhotoPicker.builder()
                .setPhotoCount(1)
                .setShowCamera(true)
                .setShowGif(false)
                .setPreviewEnabled(true)
                .start(this, PhotoPicker.REQUEST_CODE);
    }

    @OnClick(R.id.iv_location)
    protected void onLocationClick(){
        String text = getResources().getString(R.string.ensure);
        Bundle bundle = new Bundle();
        bundle.putString(EaseGMapActivity.FORWARD,text);
        toActivityForResult(EaseGMapActivity.class,bundle,REQ_LOCATION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode !=RESULT_OK || data == null) return;

        switch (requestCode){
            case PhotoPicker.REQUEST_CODE:
                tv_licence.setVisibility(View.GONE);
                ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                img = photos.get(0);
                Glide.with(this).fromFile().load(new File(img)).into(iv_licence);
                break;
            case REQ_CATEGORY:
                String ids = data.getStringExtra("ids");
                String names = data.getStringExtra("names");
                lvv_category.setTag(ids);//所选的类别id
                lvv_category.setValueText(names);
                break;
            case REQ_LOCATION:
                latitude = data.getDoubleExtra("latitude", 0);
                longitude = data.getDoubleExtra("longitude", 0);
                String address = data.getStringExtra("address");
                liv_address.value(address);

                break;
        }
    }
}
